import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import axios from 'axios'

import { fnAddDynamicMenuRoutes } from '@/router/getAsyncRouter'

Vue.use(VueRouter)
NProgress.configure({
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})

const routes = [
    {
        path: "/login",
        component: () => import('../views/login')
    },
    {
        path: "/",
        component: () => import("../views/index"),
        redirect: "/index",
        meta: [
            { name: "首页" },
        ],
        children: [
            {
                path: "/index",
                component: () => import("../views/main"),
            },
            {
                path: "/teacher",
                component: () => import("../views/admin/schooleManager/teacher"),
                meta: [
                    { name: "用户管理" },
                ],
            }, {
                path: "/permission",
                component: () => import("../views/admin/schooleManager/permission"),
                meta: [
                    { name: "权限管理" },
                ],
            }, {
                path: "/role",
                component: () => import("../views/admin/schooleManager/role"),
                meta: [
                    { name: "角色管理" },
                ],
            }, {
                path: "/menu",
                component: () => import("../views/admin/schooleManager/menu"),
                meta: [
                    { name: "菜单管理" },
                ],
            },
            {
                path: "/person",
                component: () => import("../views/comm/personalData"),
                meta: [
                    { name: "个人资料" }
                ]
            },
            {
                path: "/operlog",
                component: () => import("../views/admin/monitor/operLog"),
                meta: [
                    { name: "操作日志" }
                ]
            },
            {
                path: "/logininfor",
                component: () => import("../views/admin/monitor/logininFor"),
                meta: [
                    { name: "登录日志" }
                ]
            },
            {
                path: "/mailSend",
                component: () => import("../views/admin/mail/mailSend"),
                meta: [
                    { name: "邮件发送" }
                ]
            },
            {
                path: "/mailList",
                component: () => import("../views/admin/mail/mailList"),
                meta: [
                    { name: "邮件列表" }
                ]
            },
            {
                path: "/comment",
                component: () => import("../views/admin/commentManager/commentList"),
                meta: [
                    { name: "评论列表" }
                ]
            },
            {
                path: "/photo-list",
                component: () => import("../views/admin/photoManager/list"),
                meta: [
                    { name: "照片列表" }
                ]
            },
            {
                path: "/photo-add",
                component: () => import("../views/admin/photoManager/add"),
                meta: [
                    { name: "照片新增" }
                ]
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    duplicateNavigationPolicy: true
})

//白名单
const whiteList = ["/login"]

router.beforeEach((to, from, next) => {
    NProgress.start();
    //to 将要访问的路径
    // from 代表从哪个路径跳转而来
    //next 表示放行函数 
    // next（） 直接放行  next('/xxx') 强制跳转
    if (whiteList.indexOf(to.path) != -1) {
        //判断将要访问的路径是不是在白名单中，是则返回0
        next()
    } else {
        if (store.getters.getUser != null || store.getters.getToken != null) {
            //判断是否登录
            let routers = store.getters.getRouters
            //登录进来本地还没有存值
            if (routers == null) {
                axios.get("/menu/getMenus", {
                    headers: {
                        Authorization: localStorage.getItem("token")
                    }
                }).then(res => {
                    store.commit("SET_ROUTERS", res.data.data)
                    routers = store.getters.getRouters
                    fnAddDynamicMenuRoutes(routers)
                    next()
                })
            }

            next()
        } else {
            //代表用户未登录，跳转登录页面
            next({
                path: "/login"
            })
        }
    }
})
router.afterEach(() => {
    NProgress.done();
})

export default router

/*
    蓝色小进度条
    // 下载安装
    npm i nprogress -S

    // 基本用法
    NProgress.start();			// 开启进度条
    NProgress.done();			// 关闭进度条
*/